﻿using Microsoft.Win32;
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;

namespace ST.Common.Util
{
	public static class RegistryHelper
	{
		public static List<KeyValuePair<string, object>> GetRegValues(RegistryKey rootKey, string subKeyName)
		{
			List<KeyValuePair<string, object>> lst = new List<KeyValuePair<string, object>>();
			if (string.IsNullOrEmpty(subKeyName))
			{
				throw new ArgumentNullException("键值不能为空");
			}
			RegistryKey fullKey = rootKey.OpenSubKey(subKeyName);
			if (fullKey != null)
			{
				string[] names = fullKey.GetValueNames();
				if (names != null && names.Length > 0)
				{
					foreach (string name in names)
					{
						KeyValuePair<string, object> item = new KeyValuePair<string, object>(name, fullKey.GetValue(name));
						lst.Add(item);
					}
				}
				fullKey.Close();
			}
			return lst;
		}

		public static List<KeyValuePair<string, object>> GetRegValues(RegistryKey rootKey, string root, string company, string software, string subName)
		{
			string key = string.Empty;
			if (!string.IsNullOrEmpty(root))
			{
				key += root + "\\";
			}
			if (!string.IsNullOrEmpty(company))
			{
				key += company + "\\";
			}
			if (!string.IsNullOrEmpty(software))
			{
				key += software + "\\";
			}
			if (!string.IsNullOrEmpty(subName))
			{
				key += subName;
			}
			return GetRegValues(rootKey, key);
		}

		public static List<KeyValuePair<string, object>> GetRegValues_LocalMachine(string subKeyName)
		{
			return GetRegValues(Registry.LocalMachine, subKeyName);
		}

		public static List<KeyValuePair<string, object>> GetRegValues_LocalMachine_Software(string company, string software, string subName)
		{
			return GetRegValues(Registry.LocalMachine, "SOFTWARE", company, software, subName);
		}

		public static List<KeyValuePair<string, object>> GetRegValues_CurrentUser(string subKeyName)
		{
			return GetRegValues(Registry.CurrentUser, subKeyName);
		}

		public static List<KeyValuePair<string, object>> GetRegValues_CurrentUser_Software(string company, string software, string subName)
		{
			return GetRegValues(Registry.CurrentUser, "SOFTWARE", company, software, subName);
		}

		public static List<string> GetRegSubNames(RegistryKey rootKey, string subKeyName)
		{
			List<string> lst = new List<string>();
			if (string.IsNullOrEmpty(subKeyName))
			{
				throw new ArgumentNullException("键值不能为空");
			}
			RegistryKey fullKey = rootKey.OpenSubKey(subKeyName);
			if (fullKey != null)
			{
				string[] names = fullKey.GetValueNames();
				if (names != null && names.Length > 0)
				{
					foreach (string name in names)
					{
						KeyValuePair<string, object> item = new KeyValuePair<string, object>(name, fullKey.GetValue(name));
						//lst.Add(item);
					}
				}
				fullKey.Close();
			}
			return lst;
		}

		public static List<string> GetRegSubNames(RegistryKey rootKey, string root, string company, string software, string subName)
		{
			List<string> lst = new List<string>();
			if (string.IsNullOrEmpty(company) && string.IsNullOrEmpty(software))
			{
				throw new ArgumentNullException("键值不能为空");
			}
			string key = string.Empty;
			if (!string.IsNullOrEmpty(root))
			{
				key += root + "\\";
			}
			if (!string.IsNullOrEmpty(company))
			{
				key += company + "\\";
			}
			if (!string.IsNullOrEmpty(software))
			{
				key += software + "\\";
			}
			if (!string.IsNullOrEmpty(subName))
			{
				key += subName;
			}
			return GetRegSubNames(rootKey, key);
		}

		/// <summary>
		/// 设置开机启动
		/// </summary>
		/// <param name="appName"></param>
		/// <param name="appPath"></param>
		/// <returns></returns>
		public static bool SetAutoRun(string appName, string appPath)
		{
			bool success = false;
			try
			{
				if (string.IsNullOrEmpty(appPath))
				{
					return false;
				}
#if DEBUG
				log4net.LogManager.GetLogger(typeof(RegistryHelper)).Debug("正在设置开机启动程序：" + appPath);
#endif
				RegistryKey fullKey = Registry.LocalMachine.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", true);
				if (fullKey != null)
				{
					string[] names = fullKey.GetValueNames();
					if (string.IsNullOrEmpty(appName))
					{
						appName = Path.GetFileNameWithoutExtension(appPath);
					}
					string keyName = appName;
					if (names != null && names.Length > 0)
					{
						foreach (string name in names)
						{
							if (string.Compare(Convert.ToString(fullKey.GetValue(name)), appPath, true) == 0)//值相同
							{
								keyName = name;
								break;
							}
							if (string.Compare(name, appName, true) == 0)//键相同
							{
								keyName = name;
								break;
							}
						}
					}
					fullKey.SetValue(keyName, appPath);
					fullKey.Close();
					success = true;
				}
			}
			catch (Exception ex)
			{
				log4net.LogManager.GetLogger(typeof(RegistryHelper)).Error(ex);
			}
			return success;
		}
	}
}
